<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="竞猜订单" prop="isQuiz">
        <el-select v-model="queryParams.isQuiz" clearable size="small">
          <el-option label="是" value="1" />
          <el-option label="否" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="商铺编号"  v-hasPermi="['system:shop:field']" prop="shopId">
        <el-input v-model="queryParams.shopId" placeholder="请输入商铺编号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="订单状态" clearable size="small">
          <el-option v-for="dict in statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="售后状态" prop="afterSaleState">
        <el-select v-model="queryParams.afterSaleState" placeholder="售后状态" clearable size="small">
          <el-option v-for="dict in afterStatusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="运单公司" prop="courierName">
        <el-input v-model="queryParams.courierName" placeholder="请输入运单公司名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="运单编号" prop="courierNumber">
        <el-input v-model="queryParams.courierNumber" placeholder="请输入运单编号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品名称" prop="spuTitle">
        <el-input v-model="queryParams.spuTitle" placeholder="请输入商品名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="订单创建时间">
        <el-date-picker v-model="queryParams.createTimeBegin" type="datetime" format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
        至
        <el-date-picker v-model="queryParams.createTimeEnd" type="datetime" format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['admin:order:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['admin:order:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['admin:order:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['admin:order:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <!-- <el-table-column label="用户编号" align="center" prop="userId" /> -->
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="收货地址" align="center" prop="addrInfo" />
      <el-table-column label="竞猜订单" align="center" prop="isQuiz">
        <template slot-scope="scope">
          {{ scope.row.isQuiz ==0? '否': '是' }}
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="spu">
        <template slot-scope="scope">
          {{ (scope.row.spu || []).title }}
        </template>
      </el-table-column>
      <el-table-column label="订单总金额" align="center" prop="totalPrice" />
      <el-table-column label="订单状态" align="center" prop="status" :formatter="statusFormat" />
      <!-- <el-table-column label="订单运费" align="center" prop="shopping" /> -->
      <!-- <el-table-column label="订单消证" align="center" prop="disappear" /> -->
      <!-- <el-table-column label="商户编号" align="center" prop="shopId" /> -->
      <el-table-column label="运单公司名称" align="center" prop="courierName" />
      <el-table-column label="运单编号" align="center" prop="courierNumber" />
      <!-- <el-table-column label="订单系统编号" align="center" prop="outTradeNo" /> -->
      <!-- <el-table-column label="支付类型" align="center" prop="payType">
        <template slot-scope="scope">
          {{scope.row.payType == 0 ? '支付宝' : scope.row.payType == 1? '微信': '' }}
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" align="center" prop="createTime" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleUpdate(scope.row)" v-hasPermi="['admin:order:updateCourierNumber']" v-show="scope.row.status == 1">发货</el-button>
          <el-button type="text" size="mini" @click="handleApproval(scope.row)" v-hasPermi="['admin:order:approvalOrder'] " v-show="scope.row.status == 7 && scope.row.afterSaleState == 1">审批退款</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['admin:order:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['admin:order:remove']">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户编号" :disabled="true" />
        </el-form-item>
        <el-form-item label="竞猜订单" prop="isQuiz">
          <el-select v-model="form.isQuiz" clearable size="small" :disabled="true">
            <el-option label="是" value="1" />
            <el-option label="否" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="订单总金额" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入订单总金额" :disabled="true" />
        </el-form-item>
        <!-- <el-form-item label="订单运费" prop="shopping">
          <el-input v-model="form.shopping" placeholder="请输入订单运费" :disabled="true" />
        </el-form-item> -->
        <el-form-item label="订单消证" prop="disappear">
          <el-input v-model="form.disappear" placeholder="请输入订单消证" :disabled="true" />
        </el-form-item>
        <el-form-item label="运单公司名称" prop="courierName">
          <el-input v-model="form.courierName" placeholder="请输入运单公司名称" />
        </el-form-item>
        <el-form-item label="运单编号" prop="courierNumber">
          <el-input v-model="form.courierNumber" placeholder="请输入运单编号" />
        </el-form-item>
        <el-form-item label="订单系统编号" prop="outTradeNo">
          <el-input v-model="form.outTradeNo" placeholder="请输入订单系统编号" :disabled="true" />
        </el-form-item>
        <br />
        <el-form-item label="收货人姓名" prop="contactName">
          <el-input v-model="form.contactName" placeholder="收货人姓名" :disabled="true" />
        </el-form-item>
        <el-form-item label="收货人电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="收货人电话" :disabled="true" />
        </el-form-item>
        <el-form-item label="收货地址" prop="addrInfo">
          <el-input v-model="form.addrInfo" placeholder="收货地址" :disabled="true" />
        </el-form-item>
        <br />
        <el-form-item label="商品标题" prop="spu">
          <el-input v-model="(form.spu || {}).title" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品封面" prop="spu">
          <el-image style="width: 100px; height: 100px" :src="(form.spu || {}).logo" :preview-src-list="[(form.spu || {}).logo]">
          </el-image>
        </el-form-item>
        <el-form-item label="购买数量" prop="spuNumber">
          <el-input v-model="form.spuNumber" placeholder="请输入购买数量" :disabled="true" />
        </el-form-item>
        <el-form-item label="是否评价" prop="isAppraise">
          <el-select v-model="form.isAppraise" clearable size="small" :disabled="true">
            <el-option label="是" value="1" />
            <el-option label="否" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="规格" prop="skuName">
          <el-input v-model="(form.sku || {}).skuName" placeholder="请输入购买数量" :disabled="true" />
        </el-form-item>
        <el-form-item label="价格" prop="skuMoney">
          <el-input v-model="(form.sku || {}).skuMoney" placeholder="请输入购买数量" :disabled="true" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="approvalTitle" :visible.sync="approvalOpen" width="500px" append-to-body>
      <el-form ref="form" :model="refundForm" label-width="100px">
        <el-form-item label="审批意见: " prop="afterSaleState">
          <el-switch v-model="refundForm.afterSaleState" active-value="2" active-text="同意退款" inactive-value="3" inactive-text="拒绝退款" active-color="#13ce66" inactive-color="#666">
          </el-switch>
        </el-form-item>
        <el-form-item label="拒绝原因: " prop="afterSaleRemark" v-if="refundForm.afterSaleState == '3'">
          <el-input v-model="refundForm.afterSaleRemark" placeholder="请输入拒绝原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRefundForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder, approvalOrder, updateCourierNumber } from "@/api/admin/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //审批界面的 标题，开关
      approvalTitle: "",
      approvalOpen: false,
      // 状态数据字典
      statusOptions: [],
      //售后状态
      afterStatusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,

        isQuiz: null,

        totalPrice: null,

        status: null,

        shopping: null,

        disappear: null,

        shopId: null,

        courierName: null,

        courierNumber: null,

        outTradeNo: null,

        payType: null,

        userName: null,
        phone: null,
        spuTitle: null,
        createTimeBegin: null,
        createTimeEnd: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户编号不能为空", trigger: "blur" }
        ],

      },
      //退款审批表单参数
      refundForm: {},
    };
  },
  created() {
    this.getDicts("order_state").then((response) => {
      this.statusOptions = response.data;
    });
    //afterStatusOptions
    this.getDicts("approval_state").then((response) => {
      this.afterStatusOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      if (this.queryParams.createTimeBegin)  this.queryParams.createTimeBegin = this.queryParams.createTimeBegin.getTime();
      if (this.queryParams.createTimeEnd)  this.queryParams.createTimeEnd = this.queryParams.createTimeEnd.getTime();
      console.warn("请求参数", this.queryParams);
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        userId: null,

        isQuiz: null,

        totalPrice: null,

        status: 0,

        shopping: null,

        disappear: null,

        shopId: null,

        courierName: null,

        courierNumber: null,

        outTradeNo: null,

        payType: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        userName: null,
        phone: null,
        spuTitle: null,
        createTimeBegin: null,
        createTimeEnd: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.createTimeBegin = null;
      this.queryParams.createTimeEnd = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOrder(id).then(response => {
        this.form = response.data;
        this.form.isQuiz = this.form.isQuiz + '';
        this.form.isAppraise = this.form.isAppraise + '';
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 审批按钮操作 */
    handleApproval(row) {
      this.reset();
      const id = row.id;
      getOrder(id).then(response => {
        this.refundForm = response.data;
        this.refundForm.afterSaleState = "3";
        this.approvalOpen = true;
        this.approvalTitle = "退款审批";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCourierNumber(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 退款审批提交按钮 */
    submitRefundForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.warn("this.refundForm", this.refundForm)
          approvalOrder(this.refundForm).then(response => {
            this.msgSuccess("审批成功");
            this.approvalOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除订单编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportOrder(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },
    /** 打开审批界面 */
    openApproval(row) {
      this.reset();
      const id = row.id;
      this.approvalOpen = true;
    },

    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    //afterStatusOptions
    // 字典状态字典翻译
    AfterstatusFormat(row, column) {
      return this.selectDictLabel(this.afterStatusOptions, row.approvalResult);
    },
  }
};
</script>
